
import {
    baseRequest,
    getDataList,
    getData,
    getAddForm,
    delData,
    exportData,
    upgrade,
} from "@/api/dynamic/dynamic";
import { getToken } from "@/utils/auth";
import { deepClone } from "@/utils";
import { handleBatchDownload } from "@/utils/zipdownload";
import { addDownload, getDownloadUrl } from "@/api/pms/download";
export default {
    data() {
        return {

            loading: false,
            // 弹出层标题
            title: "",
            subtitle: "",
            open: false,
            topen: false,
            subopen: false,
            form: {},
            rules: {},
            addFormData: [],
            addurl: "",
            subtopen: false,
            //所选项id
            ids: [],
            // 所选项
            selected: [],
            total: 0,
            options: {
                // table 的参数
                stripe: true, // 是否为斑马纹 table
                loading: false, // 是否添加表格loading加载动画
                highlightCurrentRow: true, // 是否支持当前行高亮显示
                mutiSelect: true, // 是否支持列表项选中功能
                expandColumn: "name",
                showSummary: false,
            },
            operates: {
                // 列操作按钮
                width: 150,
                list: [],
            },
            // 用户导入参数
            upload: {
              // 是否显示弹出层（用户导入）
              open: false,
              // 弹出层标题（用户导入）
              title: "",
              // 是否禁用上传
              isUploading: false,
              // 是否更新已经存在的用户数据
              updateSupport: 0,
              // 设置上传的请求头部
              headers: { Authorization: "Bearer " + getToken() },
              // 上传的地址
              url: process.env.VUE_APP_BASE_API + "/system/user/importData",
            },
            editall: false,
            showAdd: false,
            Timeer: 0,
            progress: {
              open: false,
              percent: 0,
            },
        }
    },
    methods: {

        // 按钮点击事件操作
        clickEvent(obj) {
            if (obj.name == "全编辑") {
                this.editall = true;
                if (obj.pageParams) {
                    var pageParams = JSON.parse(obj.pageParams.replace(/'/g, '"'));
                    this.addurl = pageParams.submiturl;
                    this.showAdd = pageParams.showAdd?true:false
                }
                // this.editall = true
                if (this.$refs.treeTable) {
                    this.$refs.treeTable.editall = this.editall;
                    // this.$refs.treeTable.getEditForm(obj.url)
                } else {
                    // console.log(this.$refs.dyTable)
                    this.$refs.dyTable.editall = this.editall;
                    this.$refs.dyTable.showAdd = this.showAdd;
                    // this.$refs.dytable.getEditForm(obj.url)
                }
                return;
            }
            if (obj.type === "2") {
                if (obj.url&&obj.url.indexOf("http") > -1) {
                    window.open(obj.url);
                    return false;
                }
                if (this.ids.length !== 1 && obj.location !== "2") {
                    this.msgError("不能对没有选中或者选中多个的数据进行此项操作！");
                    return false;
                }
                if (obj.url) {
                    let pageParams = JSON.parse(obj.pageParams.replace(/'/g, '"'));
                    let params = {};
                    var pid = "";
                    if (obj.location !== "2") pid = this.ids[0];
                    else pid = this.projectid;
                    pageParams.url = obj.url;
                    pageParams.title = obj.name;
                    pageParams.projectid = this.projectid;
                    params.pageParams = pageParams;
                    this.$router.push({
                        path: "/dynamic/" + pageParams.route + "/" + pid,
                        query: params,
                    });
                }
            } else {
                this.table = "";
                this.toolbar = "";
                this.title = "";
                this.command = "";
                let pageParams = JSON.parse(obj.pageParams.replace(/'/g, '"'));
                // if (pageParams.condition) {
                //   if (!this.jugeCondition(pageParams.condition).flag) {
                //     return false;
                //   }
                // }
                // console.log(pageParams)
                if (pageParams.multi != undefined) this.formMulti = pageParams.multi;
                this.loading = true;
                if (pageParams.handle == "handleDelete") {
                    this.handleDelete(obj.url, obj.description);
                } else if (pageParams.handle == "handleExport") {
                    this.handleExport(obj.url);
                } else if (pageParams.handle == "handleImport") {
                    this.handleImport(obj.url, obj.description);
                } else if (pageParams.handle == "handleAdd") {
                    if (pageParams.type && pageParams.type.indexOf("table") > -1) {
                        this.handleAddExited(obj);
                    } else {
                        if (obj.sub)
                            this.handleAdd(
                                obj.url,
                                pageParams.addForm,
                                obj.description,
                                true
                            );
                        else this.handleAdd(obj.url, pageParams.addForm, obj.description);
                    }
                } else if (pageParams.handle == "handleAddChild") {
                    if (obj.sub)
                        this.handleAddChild(
                            obj.url,
                            pageParams.addForm,
                            obj.description,
                            true
                        );
                    else
                        this.handleAddChild(obj.url, pageParams.addForm, obj.description);
                } else if (pageParams.handle == "handleAddAssign") {
                    this.handleAddAssign(obj);
                } else if (pageParams.handle == "handlePortal") {
                    this.handlePortal(obj.name, pageParams.portal);
                } else if (pageParams.handle == "handleConfirm") {
                    this.handleConfirm(
                        obj.url,
                        pageParams.method,
                        obj.description,
                        pageParams.condition
                    );
                } else if (pageParams.handle == "handleDownload") {
                    this.handleDownload();
                }else if (pageParams.handle == "handleUpgrade") {
                    this.handleUpgrade(obj.url);
                } else if (pageParams.handle == "handleUpdate") {
                    if (obj.sub)
                        this.handleUpdate(
                            obj.url,
                            pageParams.addForm,
                            obj.description,
                            true
                        );
                    else this.handleUpdate(obj.url, pageParams.addForm, obj.description);
                } else if (pageParams.handle == "handleBatch") {
                    this.handleBatch(obj, pageParams.addForm, pageParams.table);
                } else if (pageParams.handle == "handleCopy") {
                    // 新增版本
                    this.$confirm("确认新增版本?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                        .then(() => {
                            getDataList(obj.url, { id: this.$route.params.Id }).then(
                                (response) => {
                                    if (response.code == 200) {
                                        this.msgSuccess(response.msg);
                                        let i = Number(this.activeIndex);
                                        this.handleOpen(this.treeMenu[i], i);
                                        return true;
                                    } else {
                                        this.msgError(response.msg);
                                        return true;
                                    }
                                }
                            );
                        })
                        .catch(() => {
                            this.msgInfo("已取消");
                        });
                } else if (pageParams.handle == "handleOpen") {
                    let i = this.params.currentIndex;
                    this.handleOpen(obj, i);
                } else if (pageParams.handle == "handleTable") {
                    let i = this.params.currentIndex;
                    this.openTable({
                        url: obj.url,
                        str: pageParams,
                        id: this.ids,
                        title: obj.description,
                    });
                }
                this.loading = false;
            }
        },
        /** 新增按钮操作 */
        handleAdd(url, str, title, sub = false) {
            this.addurl = this.getUrl(url, this.$data);
            this.getFormData(str, true);
            if (!sub) {
                if (this.root) {
                    if (this.root.dataInfo) {
                        this.form = {
                            projectid: this.root.dataInfo.projectid
                                ? this.root.dataInfo.projectid
                                : this.root.dataInfo.id,
                            parentid: this.root.parentid,
                        };
                        if (this.root.dataInfo.projectid) this.form["pmsid"] = this.projectid;
                    }else{
                        this.form={}
                    }
                } else {
                    if (this.dataInfo) {
                        this.form = {
                            projectid: this.dataInfo.projectid
                                ? this.dataInfo.projectid
                                : this.dataInfo.id,
                            parentid: this.parentid,
                        };
                        if (this.dataInfo.projectid) this.form["pmsid"] = this.projectid;
                    }
                }
                if (this.ids.length > 0) this.form["toid"] = Number(this.ids[0]);
                this.open = true;
                this.title = title;
            } else {
                this.form = {
                    projectid: this.dataInfo.projectid
                        ? this.dataInfo.projectid
                        : this.dataInfo.id,
                    parentid: this.parentid,
                    toid: this.form.toid,
                    pmsid: this.form.pmsid,
                };
                if (this.ids.length > 0) this.form["fromid"] = Number(this.ids[0]);
                if (this.dataInfo.projectid) this.form["pmsid"] = this.projectid;
                this.subopen = true;
                this.subtitle = title;
            }
            // for (let item of this.addFormData) {
            //     if (item.status == "1") {
            //         this.$set(this.form, item.prop, item.defaultValue);
            //     }
            // }
        },
        /**  添加已存在*/
        handleAddExited(obj) {
            let pageParams = eval("(" + obj.pageParams + ")");
            this.addurl = obj.url;
            this.table = pageParams.table;
            this.toolbar = pageParams.toolbar;
            this.command = pageParams.command;
            this.formtype = pageParams.type;
            if (pageParams.search != undefined) this.search = pageParams.search;
            this.geturl = this.getUrl(pageParams.getUrl);
            if (obj.sub) {
                this.form = {
                    projectid: this.projectid,
                    parentid: this.parentid,
                    toid: this.form.toid,
                    formid: this.ids[0],
                    pmsid: this.form.pmsid,
                };
                this.subtopen = true;
                this.subtitle = obj.description;
            } else {
                this.copy_geturl = this.geturl;
                if (this.ids.length > 0)
                    this.form = {
                        projectid: this.dataInfo.projectid
                            ? this.dataInfo.projectid
                            : this.dataInfo.id,
                        parentid: this.parentid,
                        toid: this.ids[0],
                    };
                else
                    this.form = {
                        projectid: this.dataInfo.projectid
                            ? this.dataInfo.projectid
                            : this.dataInfo.id,
                        parentid: this.parentid,
                    };
                if (this.dataInfo.projectid) this.form["pmsid"] = this.projectid;
                this.title = obj.description;
                this.topen = true;
            }
        },
        /** 添加已存在*/
        handleAddAssign(obj) {
            if (this.ids.length == 0) {
                this.msgError("不能对没有选中进行此项操作！");
                return false;
            }
            if (this.level === 0) {
                this.ids[0] = 0;
            }
            let pageParams = eval("(" + obj.pageParams + ")");
            this.addurl = obj.url;
            this.table = pageParams.table;
            this.toolbar = pageParams.toolbar;
            this.command = pageParams.command;
            this.formtype = pageParams.type;
            if (pageParams.defaultSelect) {
                let arr = pageParams.defaultSelect.split("-");
                this.defaultSelect[arr[0]] = this.selected[0][arr[1]].split(",");
            } else {
                this.defaultSelect = {};
            }
            if (pageParams.search != undefined) this.search = pageParams.search;
            // if (pageParams.getUrl.indexOf("{") != -1) {
            //   var url = pageParams.getUrl.substring(
            //     0,
            //     pageParams.getUrl.indexOf("{")
            //   );
            //   var substr = pageParams.getUrl.substring(
            //     pageParams.getUrl.indexOf("{") + 1,
            //     pageParams.getUrl.indexOf("}")
            //   );
            //   this.geturl = url + this.dataInfo[substr];
            // } else {
            //   this.geturl = pageParams.getUrl;
            // }
            // console.log(pageParams.getUrl)
            this.geturl = this.getUrl(pageParams.getUrl);
            if (obj.sub) {
                this.form = {
                    toid: this.form.toid,
                    fromid: this.ids,
                    projectid: this.projectid,
                };
                this.subtitle = obj.description;
                this.subtopen = true;
            } else {
                this.copy_geturl = this.geturl;
                this.form = {
                    pmsid: this.ids.join(","),
                    toid: this.ids.join(","),
                    projectid: this.projectid,
                };
                this.title = obj.description;
                this.topen = true;
            }
        },
        /** 增加下层按钮操作 */
        handleAddChild(url, str, title, sub = false) {
            this.form = {};
            this.addurl = url;
            if (this.ids.length !== 1) {
                this.msgError("不能对没有选中或者选中多个的数据进行此项操作！");
                return false;
            }
            if (this.level === 0) {
                this.form = {
                    projectid: this.projectid,
                    parentid: 0,
                    level: this.level,
                };
            } else {
                this.form = {
                    projectid: this.projectid,
                    parentid: this.ids[0],
                    level: this.level,
                };
            }
            this.getFormData(str, true);
            if (sub) {
                this.subtitle = title;
                this.subopen = true;
            } else {
                this.open = true;
                this.title = title;
            }
        },
        /** 修改按钮操作 */
        handleUpdate(url, str, title, sub = false) {
            if (this.ids.length != 1) {
                this.msgInfo("请选择一条数据");
                return false;
            }
            this.addurl = url;
            this.getFormData(str);
            this.form = deepClone(this.selected[0]);
            // this.open = true;
            // this.title = val.title ? val.title : "编辑";
            if (this.sub) {
                this.subopen = true;
                this.subtitle = title;
            } else {
                this.open = true;
                this.title = title;
            }
        },
        /** 删除按钮操作 */
        handleDelete(url, title) {
            const ids = this.ids;
            this.$confirm(
                `是否确认${title} ${this.selected[0].name ||
                this.selected[0].title ||
                this.selected[0].fileName ||
                this.selected[0].folderName ||
                ""
                } ${this.ids.length > 1 ? "等" : ""}?`,
                "警告",
                {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }
            ).then(()=>{
                    this.loading = true
                    delData(url, ids).then(() => {
                        this.refreshCurrent();
                        this.msgSuccess("删除成功");
                    })
                    .catch(()=>{
                        this.loading = false
                    });
                })
        },
        handleConfirm(url, method, title, condition) {
            let jugement = this.jugeCondition(condition, url, title);
            if (jugement.flag) {
                this.$confirm(jugement.info, "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        this.loading = true
                        baseRequest(jugement.route, method, {}, {}).then((response) => {
                            this.msgSuccess(response.msg);
                            this.refreshCurrent();
                            return true;
                        }).catch(()=>{
                            // console.log(err)
                            this.loading = false
                        });
                    })
            }
        },
        /** 批量文件下载 */
        handleDownload() {
            if (this.selected.length > 0) {
                let fileArray = [];
                let doc_ids = [];
                let doc_name = [];
                this.selected.forEach((item) => {
                    fileArray = fileArray.concat(item.fileList);
                    doc_ids.push(item.id);
                    doc_name.push(item.name);
                });
                if (fileArray.length > 0) {
                    if (fileArray.length > 1) {
                        handleBatchDownload(fileArray, doc_name, doc_ids, this);
                    } else {
                        if (fileArray[0].path) {
                          var downloadName = fileArray[0].fileName;
                          let url = "http://"+location.host + fileArray[0].path;
                          // 生成一个a标签
                          let link = document.createElement("a");
                          link.style.display = "none";
                          link.href = url;
                          link.download = downloadName;
                          document.body.appendChild(link);
                          link.click();
                            // var fileUrl = fileArray[0].path;
                            // var index = fileUrl.lastIndexOf("/");
                            // var bucket = fileUrl.substring(0, index);
                            // var downloadName = fileArray[0].fileName;
                            // getDownloadUrl(bucket, downloadName, fileArray[0].docId).then(
                            //     (response) => {
                            //         if (response.url !== "") {
                            //             let url = response.url;
                            //             // 生成一个a标签
                            //             let link = document.createElement("a");
                            //             link.style.display = "none";
                            //             link.href = url;
                            //             link.download = downloadName;
                            //             document.body.appendChild(link);
                            //             link.click();
                            //         } else {
                            //             this.msgError("文件地址出错");
                            //         }
                            //     }
                            // );
                        } else {
                            this.msgError("文件地址出错");
                        }
                    }
                    addDownload({
                        docId: doc_ids.toString(),
                        docName: doc_name.toString(),
                    });
                }
            }
        },
        /** 升级降级操作 */
        handleUpgrade(url) {
            const ids = this.ids;
            if (this.JugeSeletions().sameState) {
                this.$confirm(
                    `是否确认升级 ${this.selected[0].name ||
                    this.selected[0].title ||
                    this.selected[0].fileName ||
                    this.selected[0].folderName ||
                    ""
                    } ${this.ids.length > 1 ? "等" : ""}的数据项?`,
                    "警告",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }
                )
                    .then(function () {
                        return upgrade(url, ids);
                    })
                    .then(() => {
                        this.refreshCurrent();
                        this.msgSuccess("提升成功");
                    })
                    .catch(function () { });
            } else {
                this.msgError("请选择同一状态的数据！");
            }
        },
        /** 降级操作 */
        handleDemotion(url) {
            const ids = this.ids;
            if (this.JugeSeletions().sameState) {
                this.$confirm(
                    `是否确认升级 ${this.selected[0].name ||
                    this.selected[0].title ||
                    this.selected[0].fileName ||
                    this.selected[0].folderName ||
                    ""
                    } ${this.ids.length > 1 ? "等" : ""}的数据项?`,
                    "警告",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }
                )
                    .then(function () {
                        return upgrade(url, ids);
                    })
                    .then(() => {
                        this.refreshCurrent();
                        this.msgSuccess("降级成功");
                    })
                    .catch(function () { });
            } else {
                this.msgError("请选择同一状态的数据！");
            }
        },
        /** 导出按钮操作 */
        handleExport(url) {
            const queryParams = this.queryParams;
            this.$confirm("是否确认导出所有数据项?", "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(()=>{
                    return baseRequest(this.getUrl(url),'get', queryParams);
                })
                .then((response) => {
                    this.download(response.msg);
                })
                .catch(function () { });
        },
        /** 导入按钮操作 */
        handleImport(url, title) {
            this.upload.title = title;
            this.upload.url = process.env.VUE_APP_BASE_API + this.getUrl(url);
            this.upload.open = true;
        },
        handlePortal(title, portal) {
            this.portaldialog.open = true;
            this.portaldialog.title = title;
            this.portaldialog.portal = { portal };
            this.portaldialog.root = this.ids
        },
        editChanged(val) {
            this.addurl = val.url;
            this.form = {};
            if (val.url) {
                if (val.url.indexOf("/UpdateFile") == -1) {
                    if (typeof val.id != "number") {
                        this.form = deepClone(val.id);
                        this.open = true;
                        this.title = val.title ? val.title : "编辑";
                        this.getFormData(val.str);
                    } else {
                        getData(val.url, val.id).then((response) => {
                            this.form = response.data;
                            this.open = true;
                            this.title = val.title ? val.title : "编辑";
                            this.getFormData(val.str);
                        });
                    }
                } else {
                    this.form = {
                        id: typeof val.id == "number"?val.id:val.id.id,
                        fileList: [],
                        projectid: this.projectid,
                        parentid: this.parentid,
                    };
                    this.open = true;
                    this.title = val.title ? val.title : "编辑";
                    this.getFormData(val.str);
                }
            } else {
                this.open = true;
                this.title = val.title ? val.title : "编辑";
                this.getFormData(val.str);
            }
        },
        openChanged(val) {
            this.loading = false;
            if (this.subopen) {
                this.subopen = false;
                if (this.addurl.indexOf("Restrain") > -1) this.topenChanged(true);
                if (this.addurl.indexOf("pmsbatch") > -1) this.refreshCurrent();
                if (val) {
                    this.$refs.addFormTable.getList();
                }
            } else {
                this.open = false;
                if (val) {
                    this.ids = [];
                    this.refreshCurrent();
                }
            }
            clearTimeout(this.Timeer)
        },
        topenChanged(val) {
            this.loading = false;
            if (this.subtopen) {
                this.subtopen = false;
                this.geturl = this.copy_geturl;
                this.$refs.addFormTable.getList(this.copy_geturl);
            } else {
                this.topen = false;
                if (val) {
                    this.ids = [];
                    this.getConfig(this.type);
                }
            }
            clearTimeout(this.Timeer)
        },
        //获取表单数据
        getFormData(str, flag) {
            // let formData = {};
            getAddForm({ addForm: str }).then((response) => {
                // this.addFormData = this.formDataUpdate(response.data.filter(item=>{item.status=='1'}));
                this.addFormData = this.formDataUpdate(response.data);
                if (flag) {
                    for (let item of this.addFormData) {
                        if (this.form[item.prop] == undefined && item.defaultValue) {
                            // console.log(item.defaultValue,this.dataInfo)
                            if (item.defaultValue.indexOf('$') > -1)
                                this.$set(this.form, item.prop, this.dataInfo[item.defaultValue.replace(/\$/g, '')]);
                            else
                                this.$set(this.form, item.prop, item.defaultValue)
                        }
                    }
                }
            });
        },
        formDataUpdate(data) {
            let optionData = JSON.parse(sessionStorage.getItem("optionsData"));
            let formData = data;
            formData.forEach((item, index) => {
                if (item.status === '1') {
                    if (
                        item.componentType === "el-select" ||
                        item.componentType === "el-selects" ||
                        item.componentType === "el-radio" ||
                        item.componentType === "el-checkbox-group"
                    ) {
                        if (
                            item.prop === "policyid" ||
                            item.prop === "stateid" ||
                            item.prop === "ownerid"
                        ) {
                            if(item.optionValue)
                            formData[index].optionValue = optionData[item.optionValue]
                            else
                            formData[index].optionValue = optionData[item.prop.slice(0,-2)+'Options']
                        } else {
                            if (
                                this.tempDicts[item.prop] &&
                                this.tempDicts[item.prop].length > 0 &&
                                (!item.optionValue || item.optionValue.indexOf('imurl') == -1)
                            ) {
                                formData[index].optionValue = this.selectNormalizer(
                                    deepClone(this.tempDicts[item.prop])
                                );
                                // let tempDicts = JSON.parse(sessionStorage.getItem("tempDicts"));
                                // tempDicts[item.prop] = deepClone(this.tempDicts[item.prop]);
                                sessionStorage.setItem("tempDicts", JSON.stringify(this.tempDicts));
                            } else {
                                if (item.optionValue) {
                                    if (item.optionValue.indexOf("url") > -1) {
                                        let url = this.getUrl(item.optionValue.split(":")[1]);
                                        let appendid = this.$route.params.Id;
                                        if (item.optionValue.indexOf("{") > -1) {
                                            if (this.$route.query.type.indexOf("pmswbs") > -1) {
                                                appendid = this.dataInfo.projectid;
                                            }
                                            if (
                                                this.tempDicts[item.prop + "_" + appendid] == undefined ||
                                                item.optionValue.indexOf('imurl') > -1
                                            ) {
                                                getDataList(url).then((response) => {
                                                    this.tempDicts[item.prop + "_" + appendid] =
                                                        deepClone(response.rows);
                                                    // let tempDicts = JSON.parse(
                                                    //     sessionStorage.getItem("tempDicts")
                                                    // );
                                                    // tempDicts[item.prop + "_" + appendid] = deepClone(
                                                    //     response.rows
                                                    // );
                                                    item.optionValue = deepClone(response.rows)
                                                    sessionStorage.setItem(
                                                        "tempDicts",
                                                        JSON.stringify(this.tempDicts)
                                                    );
                                                });
                                            } else {
                                                // let tempDicts = JSON.parse(
                                                //     sessionStorage.getItem("tempDicts")
                                                // );
                                                // tempDicts[item.prop + "_" + appendid] = deepClone(
                                                //     this.tempDicts[item.prop + "_" + appendid]
                                                // );
                                                item.optionValue = deepClone(this.tempDicts[item.prop + "_" + appendid])
                                                sessionStorage.setItem(
                                                    "tempDicts",
                                                    JSON.stringify(this.tempDicts)
                                                );
                                            }
                                        } else if (this.tempDicts[item.prop] == undefined) {
                                            getDataList(url).then((response) => {
                                                // if(url.indexOf('enovia')>-1){
                                                //     response = JSON.parse(response)
                                                // }
                                                if (response.rows.length > 0) {
                                                    this.tempDicts[item.prop] = deepClone(response.rows);
                                                    // let tempDicts = JSON.parse(
                                                    //     sessionStorage.getItem("tempDicts")
                                                    // );
                                                    // tempDicts[item.prop] = deepClone(response.rows);
                                                    sessionStorage.setItem(
                                                        "tempDicts",
                                                        JSON.stringify(this.tempDicts)
                                                    );
                                                    formData[index].optionValue = response.rows;
                                                    if (response.rows && response.rows.length == 1) {
                                                        // console.log(response.rows)
                                                        this.form[item.prop] = response.rows[0].id;
                                                    }
                                                }
                                            });
                                        }
                                    } else {
                                        this.getDicts(item.optionValue).then((response) => {
                                            if (response.data.length > 0) {
                                                this.tempDicts[item.prop] = deepClone(response.data);
                                                formData[index].optionValue = this.selectNormalizer(
                                                    response.data
                                                );
                                            }
                                        });
                                    }
                                } else if (item.show && this.form.id) {
                                    getDataList(this.getUrl(item.show, this.form)).then(
                                        (response) => {
                                            if (response.code == 200) {
                                                item.optionValue = response.rows;
                                            }
                                        }
                                    );
                                }
                            }
                        }
                    }
                    if (item.componentType === "el-cascader") {
                        let optionarr = item.optionValue.split("?");
                        if (optionarr.length > 1) {
                            this.optionNormalizer(optionData[optionarr[0]]);
                            let typearr = [];
                            optionData[optionarr[0]].forEach((titem) => {
                                if (titem.value === parseInt(optionarr[1])) {
                                    typearr.push(titem);
                                    formData[index].optionValue = typearr;
                                }
                            });
                        } else {
                            formData[index].optionValue = this.optionNormalizer(
                                optionData[item.optionValue]
                            );
                        }
                    }
                    if (item.componentType === "el-add-existing") {
                        if (item.prop == "productName" || item.prop == "productCode") return;
                        let appendid = this.$route.params.Id;
                        if (this.$route.query.type && this.$route.query.type.indexOf("pmswbs") > -1) {
                            appendid = this.dataInfo.projectid;
                        }
                        let pageParams = eval("(" + item.show + ")");
                        let type = pageParams.type;
                        let geturl = pageParams.getUrl;
                        if (geturl.indexOf("{") > -1) {
                            if (this.tempDicts[item.prop + "_" + appendid]) {
                                // let tempDicts = JSON.parse(sessionStorage.getItem("tempDicts"));
                                // tempDicts[item.prop + "_" + appendid] = deepClone(
                                //     this.tempDicts[item.prop + "_" + appendid]
                                // );
                                item.optionValue = deepClone(
                                    this.tempDicts[item.prop + "_" + appendid]
                                );
                                sessionStorage.setItem("tempDicts", JSON.stringify(this.tempDicts));
                            } else {
                                geturl = this.getUrl(pageParams.getUrl, { projectId: appendid });
                                getDataList(geturl).then((res) => {
                                    // if (geturl.indexOf("enovia") > -1) {
                                    //     res = JSON.parse(res)
                                    // }
                                    if (res.rows && res.rows.length > 0) {
                                        // let tempDicts = JSON.parse(sessionStorage.getItem("tempDicts"));
                                        // tempDicts[item.prop + "_" + appendid] = deepClone(res.rows);
                                        this.tempDicts[item.prop + "_" + appendid] = deepClone(
                                            res.rows
                                        );
                                        item.optionValue = deepClone(
                                            this.tempDicts[item.prop + "_" + appendid]
                                        );
                                        sessionStorage.setItem(
                                            "tempDicts",
                                            JSON.stringify(this.tempDicts)
                                        );
                                        if (type == "split_tree_table") {
                                            for (let i of res.rows) {
                                                getDataList(this.getUrl(pageParams.expandUrl, i), {
                                                    loginUser:
                                                        sessionStorage.getItem("loginUser") ||
                                                        this.$store.state.user.userid,
                                                }).then((response) => {
                                                    if (pageParams.expandUrl.indexOf("enovia") > -1) {
                                                        response = JSON.parse(response)
                                                    }
                                                    // let tempDicts = JSON.parse(sessionStorage.getItem("tempDicts"));
                                                    this.tempDicts[item.prop + "_" + appendid] = this.tempDicts[
                                                        item.prop + "_" + appendid
                                                    ].concat(response.rows);
                                                    item.optionValue = deepClone(
                                                        this.tempDicts[item.prop + "_" + appendid]
                                                    );
                                                    sessionStorage.setItem(
                                                        "tempDicts",
                                                        JSON.stringify(this.tempDicts)
                                                    );
                                                    // this.tempDicts[item.prop + "_" + appendid] = deepClone(
                                                    //     tempDicts[item.prop + "_" + appendid]
                                                    // );
                                                });
                                            }
                                        }
                                    }
                                });
                            }
                        } else {
                            // geturl = this.getUrl(pageParams.getUrl);
                            if (this.tempDicts[item.prop] == undefined) {
                                getDataList(geturl).then((res) => {
                                    if (geturl.indexOf("enovia") > -1) {
                                        res = JSON.parse(res)
                                    }
                                    // let tempDicts = JSON.parse(sessionStorage.getItem("tempDicts"));
                                    // console.log(res)
                                    let rows = res.rows
                                    // if() rows= res.rows
                                    if (!res.rows && res.data) rows = res.data
                                    if (rows.length > 0) {
                                        this.tempDicts[item.prop] = deepClone(rows);
                                        item.optionValue = deepClone(this.tempDicts[item.prop]);
                                        sessionStorage.setItem(
                                            "tempDicts",
                                            JSON.stringify(this.tempDicts)
                                        );
                                        if (type == "split_tree_table") {
                                            for (let i of res.rows) {
                                                getDataList(this.getUrl(pageParams.expandUrl, i), {
                                                    loginUser:
                                                        sessionStorage.getItem("loginUser") ||
                                                        this.$store.state.user.userid,
                                                }).then((response) => {
                                                    if (pageParams.expandUrl.indexOf("enovia") > -1) {
                                                        response = JSON.parse(response)
                                                    }
                                                    this.tempDicts[item.prop] = this.tempDicts[item.prop].concat(
                                                        response.rows
                                                    );
                                                    item.optionValue = deepClone(this.tempDicts[item.prop]);
                                                    sessionStorage.setItem(
                                                        "tempDicts",
                                                        JSON.stringify(this.tempDicts)
                                                    );
                                                    // this.tempDicts[item.prop] = deepClone(tempDicts[item.prop]);
                                                });
                                            }
                                        }
                                        // else {
                                        //     item.optionValue = deepClone(tempDicts[item.prop]);
                                        //     sessionStorage.setItem(
                                        //         "tempDicts",
                                        //         JSON.stringify(tempDicts)
                                        //     );
                                        //     this.tempDicts[item.prop] = deepClone(tempDicts[item.prop]);
                                        // }
                                    }
                                });
                            } else {
                                item.optionValue = deepClone(this.tempDicts[item.prop]);
                                // let tempDicts = JSON.parse(sessionStorage.getItem("tempDicts"));
                                // tempDicts[item.prop] = deepClone(this.tempDicts[item.prop])
                                sessionStorage.setItem(
                                    "tempDicts",
                                    JSON.stringify(this.tempDicts)
                                );
                            }
                        }
                        // let formtype = pageParams.type;
                        // getDataList(geturl).then(res=>{
                        //   console.log(res)
                        // })
                    }
                }
                if (item.prop == 'dynamic') {
                    getDataList(this.getUrl(item.optionValue, this.$data)).then(res => {
                        formData.splice(index, 1, ...res.data)
                    })
                }
            });
            return formData;
        },
        /** 转换数据结构 */
        normalizer(node) {
            return {
                id: node.id,
                label: node.name,
                parentid: node.parentid,
                url: node.url,
                pageParams: node.pageParams,
            };
        },
        /** 转换选项数据结构 */
        optionNormalizer(data) {
            data.forEach((item, index) => {
                if (item) {
                    data[index] = {
                        value: item.id,
                        label: item.systemName ? item.description : item.name,
                        parentid: item.parentid,
                    };
                }
            });
            return this.handleTree(data, "value");
        },
        /** 转换选项数据结构 */
        selectNormalizer(data) {
            data.forEach((item, index) => {
                if (item) {
                    data[index] = {
                        id: item.dictValue || item.id,
                        description: item.dictLabel || item.name || item.forderName || item.description,
                    };
                }
            });
            return data;
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
          this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
          this.upload.open = false;
          this.upload.isUploading = false;
          this.$refs.upload.clearFiles();
          this.$alert(response.msg, "导入结果", {
            dangerouslyUseHTMLString: true,
            customClass: "import-result",
          });
          this.getList();
        },
        // 提交上传文件
        submitFileForm() {
          this.$refs.upload.submit();
        },
    }
}
